package com.cqust.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpSession;

public class MyInterceptor implements HandlerInterceptor, MyInterceptor1 {
    @Override
    public boolean preHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler) throws Exception {
        System.out.println("MyInterceptor preHandle");
        String uri   = request.getRequestURI();
        if(uri.contains("login") || uri.contains("register") || uri.contains("logout")) {
            return true;
        }
        HttpSession session = request.getSession();
        if(session.getAttribute("USER") != null){
            return true;
        }else{
            request.getRequestDispatcher("/index.jsp").forward(request, response);
        }
        return false;
    }

    @Override
    public  void postHandle(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, java.lang.Exception ex) throws Exception {
        System.out.println("MyInterceptor postHandle");
    }

    @Override
    public void afterCompletion(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, Object handler, java.lang.Exception ex) throws Exception {
        System.out.println("MyInterceptor afterCompletion");
    }
}
